package com.ciaojian.store.controller;

import cn.hutool.core.date.DateUtil;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.OrderServiceImpl;
import com.ciaojian.store.service.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @author Atlakyn
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private UserServiceImpl userService;


    @ApiOperation("用户数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店Id", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "查询类型\n 0月 1周 2日", paramType = "query")
    })
    @GetMapping("/customer-data")
    public Result newCustomerNum(@NotNull(message = "门店Id不能为空") Integer storeId,
                                 @NotNull(message = "查询类型不能为空") Integer type) {
        Date curDate = new Date();
        Date beginDate;
        Map<String, Object> resultMap = new HashMap<>(3);
        // 新增消费用户数
        List<Map<String, Integer>> newCustomer;
        // 消费用户性别比例
        List<Map<String, String>> genderRateOfCustomer;
        // 消费用户属性占比
        List<Map<String, Integer>> getGenderRateOfConsumer;
        // 0月 1周 2日
        switch (type) {
            case 0:
                newCustomer = orderService.getBaseMapper().increaseOfMonth(curDate, storeId);
                beginDate = DateUtil.offsetMonth(curDate, -6);
                break;
            case 1:
                newCustomer = orderService.getBaseMapper().increaseOfWeek(curDate, storeId);
                beginDate = DateUtil.offsetWeek(curDate, -4);
                break;
            case 2:
                newCustomer = orderService.getBaseMapper().increaseOfLastWeek(curDate, storeId);
                beginDate = DateUtil.offsetDay(curDate, -7);
                break;
            default:
                beginDate = DateUtil.offsetMonth(curDate, -6);
                newCustomer = new ArrayList<>();
        }
        resultMap.put("newCustomer", newCustomer);
        genderRateOfCustomer = userService.getBaseMapper().getGenderRateOfConsumer(
                storeId,
                beginDate,
                curDate);
        resultMap.put("genderRate", genderRateOfCustomer);
        getGenderRateOfConsumer = userService.getBaseMapper().getCustomerTypeRateOfConsumer(
                storeId,
                beginDate,
                curDate
        );
        resultMap.put("customerTypeRate", getGenderRateOfConsumer);
        return Result.ok(resultMap);
    }


}
